#ifndef NET_UDP_SESSION_H
#define NET_UDP_SESSION_H

#include "Threaded.h"
#include "RecursiveLockingMutex.h"
#include "UdpConnection.h"
#include <boost/asio.hpp>
#include <vector>

#define MPN_UDP_ALLOCATE_SEND_BUFFER true
#define MPN_UDP_PKT_BUFFER_MAX	1024

using boost::asio::ip::udp;
using namespace mp_base;

namespace mp_net
{

	typedef boost::shared_ptr<boost::asio::io_service> spIoService;

	class UdpSession : public mp_base::Threaded,
		public mp_base::RecursiveLockingMutex
	{
	public:

		//Constructor(s) and Destructor(s)
		UdpSession(spIoService ioService);
		virtual ~UdpSession();

		//General operations

		virtual bool Start(){return StartThread();}
		virtual bool Stop(bool wait=false){return StopThread(wait);}

		//Worker thread operations

		virtual void ThreadInitialization() = 0;
		virtual void ThreadUpdate() = 0;
		virtual void ThreadShutdown() = 0;


	protected:

		virtual void ParseInputBuffer()=0;
		virtual void BuildOutputBuffer()=0;

		//UDP Message Operations
		std::size_t ReadDataFromSocket(char* data, udp::endpoint& sender, 
			const int bufferMax = MPN_UDP_PKT_BUFFER_MAX, const int index = 0);
		void WriteDataToSocket(char* data, const udp::endpoint& target, 
			const int bufferMax = MPN_UDP_PKT_BUFFER_MAX, const int index = 0);


		spIoService					m_ioService;
		unsigned char				m_id;
		unsigned char				m_totalPlayers;
		/*! Communication Socket */
		udp::socket					m_socket;
	};
}
#endif